# 1、打开文件
# file1 = open("file1.txt", "r", encoding="utf-8")
# print(file1.readlines())  # 读取全部行
# file1.close()  # 关闭文件

# 2、用with打开文件
# open的第一个参数是文件名。第二个(mode 打开模式)决定了这个文件如何被打开。
# 如果你想读取文件，传入r
# 如果你想读取并写入文件，传入r+
# 如果你想覆盖写入文件，传入w
# 如果你想在文件末尾附加内容，传入a
with open("file1.txt", "r", encoding="utf-8") as file2:
    page = 0
    while True:
        page += 1
        line = file2.readline()  # 读取一行
        if line == "":
            break
        print(page, line, "\n")

# 3、写入文件
# with open("file2.txt", "w", encoding="utf-8") as file3:
#     file3.write("第一个python程序写示例")

# 4、读取文件
# with open("file1.txt", "r", encoding="utf-8") as file4:
#     file4.seek(2)  # 从第2个字符开始读
#     print(file4.read(8))  # 读取8个字符
